import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  UseGuards,
} from "@nestjs/common";
import { CharacterService } from "../services/character.service";
import { AuthGuard } from "../guards/auth.guard";

@Controller("character")
@UseGuards(AuthGuard)
export class CharacterController {
  constructor(private readonly characterService: CharacterService) {}

  /**
   * 获取角色信息
   * GET /character/:id
   */
  @Get(":id")
  async getCharacterInfo(@Param("id") id: string) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    try {
      const character = await this.characterService.getCharacterInfo(
        characterId
      );
      if (!character) {
        return { code: -1, msg: "角色不存在" };
      }

      return { code: 0, data: character };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 获取称号列表
   * GET /character/titles
   */
  @Get("titles/list")
  async getTitleList() {
    try {
      const titles = await this.characterService.getTitleList();
      return { code: 0, data: titles };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 修改角色名称
   * PUT /character/:id/name
   */
  @Put(":id/name")
  async modifyCharacterName(
    @Param("id") id: string,
    @Body() body: { name: string }
  ) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    const { name } = body;
    if (!name) {
      return { code: -1, msg: "名称不能为空" };
    }

    try {
      const result = await this.characterService.modifyCharacterName(
        characterId,
        name
      );
      return { code: 0, data: result };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 获取副将战斗技能
   * GET /character/:id/battle-skills
   */
  @Get(":id/battle-skills")
  async getSubordinateBattleSkills(@Param("id") id: string) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    try {
      const skills = await this.characterService.getSubordinateBattleSkills(
        characterId
      );
      if (!skills) {
        return { code: -1, msg: "角色不存在" };
      }

      return { code: 0, data: skills };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 修改称号
   * PUT /character/:id/title
   */
  @Put(":id/title")
  async modifyTitle(
    @Param("id") id: string,
    @Body() body: { titleId: string; type: number }
  ) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    const { titleId, type } = body;
    if (!titleId || type === undefined) {
      return { code: -1, msg: "参数不完整" };
    }

    try {
      const result = await this.characterService.modifyTitle(
        characterId,
        titleId,
        type
      );
      return { code: 0, data: result };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 获取副将装备信息
   * GET /character/:id/equipment
   */
  @Get(":id/equipment")
  async getSubordinateEquipment(@Param("id") id: string) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    try {
      const equipment = await this.characterService.getSubordinateEquipment(
        characterId
      );
      return { code: 0, data: equipment };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 获取技能列表
   * GET /character/:id/skills
   */
  @Get(":id/skills")
  async getSkillList(@Param("id") id: string) {
    const characterId = parseInt(id);
    if (isNaN(characterId)) {
      return { code: -1, msg: "无效的角色ID" };
    }

    try {
      const skills = await this.characterService.getSkillList(characterId);
      return { code: 0, data: skills };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 创建角色
   * POST /character
   */
  @Post()
  async createCharacter(
    @Body()
    body: {
      userId: number;
      name: string;
      roleType: number;
      sex: number;
    }
  ) {
    const { userId, name, roleType, sex } = body;

    if (!userId || !name || roleType === undefined || sex === undefined) {
      return { code: -1, msg: "参数不完整" };
    }

    try {
      const character = await this.characterService.createDefaultCharacter(
        userId,
        name,
        roleType,
        sex
      );
      return { code: 0, data: character };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }

  /**
   * 获取用户的所有角色
   * GET /character/user/:userId
   */
  @Get("user/:userId")
  async getUserCharacters(@Param("userId") userId: string) {
    const userIdNum = parseInt(userId);
    if (isNaN(userIdNum)) {
      return { code: -1, msg: "无效的用户ID" };
    }

    try {
      // 这里需要实现获取用户所有角色的方法
      const characters = await this.characterService.getUserCharacters(
        userIdNum
      );
      return { code: 0, data: characters };
    } catch (error) {
      return { code: -1, msg: (error as Error).message };
    }
  }
}
